package com.example.app2;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app2.database.UserHelper;
import com.example.app2.pojo.User;

import java.util.List;

public class SQLHelperActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ckb_married;
    private SharedPreferences preference;
    private UserHelper mHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql_helper);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ckb_married = findViewById(R.id.ckb_married);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_update).setOnClickListener(this);
        findViewById(R.id.btn_query).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mHelper=UserHelper.getInstance(this);
        mHelper.openReadLink();
        mHelper.openWriteLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHelper.closeLink();
    }

    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();

        User user=null;

        switch (v.getId())
        {
            case R.id.btn_save:
                 user=new User(name,
                         Integer.parseInt(age),
                         Float.parseFloat(height),
                         Float.parseFloat(weight),
                         ckb_married.isChecked());
                 if(mHelper.insert(user)>0)
                 {
                     Toast.makeText(this, "添加到数据库成功", Toast.LENGTH_SHORT).show();
                 }
                break;
            case R.id.btn_delete:

                if(mHelper.deleteByName(name)>0)
                {
                    Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_query:
                List<User> users = mHelper.queryByName(name);
                for (User u : users) {
                    Log.d("tang",u.toString());
                }
                break;
            case R.id.btn_update:
                user=new User(name,
                        Integer.parseInt(age),
                        Float.parseFloat(height),
                        Float.parseFloat(weight),
                        ckb_married.isChecked());
                if(mHelper.upDate(user)>0)
                {
                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
